﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using TNet;
using GameData;
using cmd;

public class ClientAction
{
    public static void ClientActionRef(BinaryReader reader)
    {
        string ActionName = reader.ReadString();
        //进行转发
        switch(ActionName)
        {
            case "注册":
                //
                RegistSend.CallBackDic["注册"](reader.ReadBoolean());
                break;
            case "角色列表":

                break;
        }

    }


}


public class CmdBase<T1,T2> {

    /// <summary>
    /// 传递毁掉函数字典
    /// -注册 Regist（true or false）
    /// </summary>
    public static Dictionary<string, Action<T2>> CallBackDic = new Dictionary<string, Action<T2>>();
    /// <summary>
    /// 功能名称
    /// 例如：注册 登录 访问
    /// </summary>
    public string ActionName;
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="str"></param>
    public CmdBase(string str)
    {
        ActionName = str;
    }
    /// <summary>
    /// 发送，并且毁掉存储到子弹
    /// </summary>
    /// <param name="sendClass">发送过去的类，需要进行包装后再发送</param>
    /// <param name="action"></param>
    public void Send(T1 sendClass,Action<T2> action)
    {
        //封装SendClass
        //使用Json封装
        string sendStr = LitJson.JsonMapper.ToJson(sendClass);
        //将回调函数
        if(CallBackDic.ContainsKey(ActionName))
        {
            CallBackDic[ActionName] = action;
        }
        else
        {
            CallBackDic.Add(ActionName, action);
        }
        if(TNManager.isConnected)
        {
            //开始发送
            BinaryWriter writer = TNManager.BeginSend(Packet.CommonServerAction);
            //写入数据
            writer.Write(ActionName);
            writer.Write(sendStr);
            //结束发送
            TNManager.EndSend();


        }


        //将回调函数保持下来

        //发送过去SendClass
    }

}

namespace cmd
{
    /// <summary>
    /// 发送帐号密码 
    /// </summary>
    public class RegistSend:CmdBase<UserInfo,bool>
    {
        public RegistSend():base("注册"){}

    }


    public class LoginSend : CmdBase<UserInfo, RoleList>
    {
        public LoginSend() : base("角色列表") { }
    }


}


